package com.example.communist_app_api.controller.uniapp;


import com.example.communist_app_api.db.dto.PhotoUserDTO;
import com.example.communist_app_api.db.dto.PhotoUserDetailDTO;
import com.example.communist_app_api.db.pojoDto.Photo;
import com.example.communist_app_api.service.PhotoService;
import com.example.communist_app_api.service.WordService;
import com.example.communist_app_api.util.CosUtils;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.PhotoCreateReq;
import com.example.communist_app_api.vo.uniapp.req.PhotoShowReq;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/uniApp/photo")
@Api("随手拍控制器")
public class UniAppPhotoController {

    @Autowired
    private PhotoService photoService;

    @Autowired
    private WordService wordService;

    @PostMapping("/create")
    @ApiOperation("新增随手拍")
    public R createPhoto(@RequestBody  PhotoCreateReq req,HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String id = claims.getSubject();

        //对需要新增的content和title进行敏感词检测
        String content = Optional.ofNullable(req.getContent())
                .orElse("");
        Boolean isSensitiveWord = wordService.detectSensitiveWord(req.getTitle().concat(content), id).getIsSensitiveWord();

        if (!isSensitiveWord){
            //当没有含敏感词时，进行照片新增
            Integer state = photoService.createPhoto(req.getContent(),Integer.valueOf(id),req.getPhotoUrlList(),req.getTitle());
            return state==1?R.ok("保存成功"):R.error("保存失败");
        }else {
            //含有敏感词，已经向用户发送系统消息
            return R.error(405,"含有敏感词");
        }

    }



    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public R createPhoto(@ApiParam("随手拍图片")
    @NotBlank @RequestBody MultipartFile multipartFile){
        String photoUrl = CosUtils.uploadFile(multipartFile, "");
        return R.ok("上传成功").put("photoUrl",photoUrl);
    }


    @ApiOperation("查询随手拍")
    @GetMapping("/select")
    public R selectAllPhoto(@Valid PhotoShowReq photoShowReq,HttpServletRequest httpServletRequest){
        Integer userIdByUniAppToken = getUserIdByUniAppToken(httpServletRequest);
        List<PhotoUserDTO> pageInfo =photoService.selectAllPhoto(photoShowReq,userIdByUniAppToken);
        return R.ok().put("pageInfo",pageInfo);
    }

    @ApiOperation("查询某个随手拍详细信息")
    @GetMapping("/selectPhotoByPhotoId")
    public R selectPhotoByPhotoId(@RequestParam(value = "photoId")  Integer photoId){
        PhotoUserDetailDTO photoUserDetailDTO = photoService.selectPhotoByPhotoId(photoId);
        return R.ok().put("photoMessage",photoUserDetailDTO);
    }



    @ApiOperation("查询某个用户的所有随手拍")
    @GetMapping("/select/{userId}")
    public R selectPhotoByUserId(@PathVariable("userId") Integer userId, @Valid PhotoShowReq photoShowReq){
        List<PhotoUserDetailDTO>  pageInfo =photoService.selectPhotoByUserId(photoShowReq,userId);
        return R.ok().put("pageInfo",pageInfo);
    }


    @ApiOperation("删除某个随手拍")
    @DeleteMapping("/delete/{photoId}")
    public R deletePhotoByPhotoId(@PathVariable("photoId") Integer photoId,HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String id = claims.getSubject();
        Photo photo = photoService.selectPhoto(photoId);
        if(!photo.getUserId().toString().equals(id)){
          return  R.error("非法删除");
        }
        Integer state  =photoService.deletePhotoByPhotoId(photoId);
        return state==1?R.ok("删除成功"):R.error("删除失败");
    }

    private Integer getUserIdByUniAppToken(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        String subject = JwtUtils.parseJWT(token).getSubject();
        int userId = Integer.parseInt(subject);
        return userId;
    }


}
